
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ListImage extends Canvas implements Runnable, CommandListener {

    private int pointer = 0;
    public boolean isRunning = true;
    private HomeScreen home; 
    private Directory directory = null;
    private Command cmdBack;
    private String filePath;
 
    public ListImage(String filePath, HomeScreen home) { 
        this.home = home;
        this.filePath = filePath;
        cmdBack = new Command("Back", Command.BACK, 2);
        addCommand(cmdBack);
        setCommandListener(this);
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        Util.resetScreen(g, this);
        Util.drawTitle("UpAnh Mobile", g, this);
        drawDirectoryChild(g);
        drawListImage(g);
        String[] left = {"Chon"};
        Util.drawMenu(left, "Thoat", g, this);
        System.gc();
    }

    private void drawDirectoryChild(Graphics g) {
        if (directory != null && directory.lstDirectorChild.size() > 0) {
            g.setFont(Util.fontContent);
            for (int i = 0; i < directory.lstDirectorChild.size(); i++) {
                if (pointer == i) {
                    g.setColor(0, 255, 0);
                } else {
                    g.setColor(0, 0, 0);
                }
                g.drawString(directory.lstDirectorChild.elementAt(i).toString(), 5, Util.fontTitle.getHeight() + 5 + i * Util.fontContent.getHeight(), 0);
            }
        }
    }

    private void drawListImage(Graphics g) {
        if (directory != null && directory.lstFileName.size() > 0) {
            int y = Util.fontTitle.getHeight() + 5 + Util.fontContent.getHeight() * directory.lstDirectorChild.size() + 5;
            for (int i = 0; i < directory.lstFileName.size(); i++) {
                try {
                    byte[] data = FileUtil.ReadFile(directory.filePath + directory.lstFileName.elementAt(i).toString());
                    Image image = Image.createImage(data, 0, data.length);
                    if (i == pointer - directory.lstDirectorChild.size()) {
                        g.setColor(0xff0000);
                        g.drawRect((getWidth() / 4) * (i % 4), y + (getWidth() / 4) * (i / 4), getWidth() / 4 - 1, getWidth() / 4 - 1);
                        image = Util.resizeImage(image, getWidth() / 4, getWidth() / 4);
                        g.drawImage(image, (getWidth() / 4) * (i % 4), y + (getWidth() / 4) * (i / 4), 0);
                    } else {
                        image = Util.resizeImage(image, getWidth() / 4 - 4, getWidth() / 4 - 4);
                        g.drawImage(image, (getWidth() / 4) * (i % 4) + 2, y + (getWidth() / 4) * (i / 4) + 2, 0);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    protected void keyPressed(int keyCode) {
        int key = getGameAction(keyCode);
        switch (key) {
            case DOWN:
                if (pointer < directory.lstDirectorChild.size() + directory.lstFileName.size() - 1) {
                    pointer++;
                }
                break;
            case RIGHT:
                if (pointer < directory.lstDirectorChild.size() + directory.lstFileName.size() - 1) {
                    pointer++;
                }
                break;
            case LEFT:
                if (pointer > 0) {
                    pointer--;
                }
                break;
            case UP:
                if (pointer > 0) {
                    pointer--;
                }
                break;
            case FIRE:
                if (pointer < directory.lstDirectorChild.size()) {
                    try {
                        directory = FileUtil.GetContentV2(directory.filePath + directory.lstDirectorChild.elementAt(pointer).toString());
                        repaint();
                    } catch (Exception ex) {
                        home.DisplayException(ex);
                    }
                } else {
                    Common.Client.SendImage(directory.filePath + directory.lstFileName.elementAt(pointer - directory.lstDirectorChild.size()));
                }
                break;
            default:
                break;
        }
        repaint();
    }

    public void run() {
        while (true) {
            if (directory == null) {
                try {
                    directory = FileUtil.GetContentV2(filePath);
                    repaint();
                } catch (Exception ex) {
                    home.DisplayException(ex);
                }
            } else {
                return;
            }
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdBack) {
            home.DisplayChoseUploadForm();
        }
    }
}
